<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * create category browsepage (article browsepage)
 * get: XXX.tpl.php
 * used from core.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
function contentPage($refPage, $page, $title, $name, $refValidate, $refCart)
{
	$pagefile = 'module/function/price.calculate.php';
	if (is_file($pagefile))
		include_once($pagefile);

	$cols = Array('category_small_image', 'category_name', 'category_description',
	 'category_metadescription', 'category_metakey', 'category_id');
	db::getInstance()->where('category_top', $title);
	db::getInstance()->where('category_active', '1');
	db::getInstance()->orderBy('category_order', 'ASC');
	$result = db::getInstance()->get("category", null, $cols);

	if ($title == '0')
	{ // Startpage
		$cols = Array('page_text', 'page_header');
		db::getInstance()->where('page_name', 'page-startpage');
		$resultx = db::getInstance()->getOne("page", null, $cols);

		$refPage->assign('header', $resultx['page_header']);
		$texte = array();

		$pageText = $resultx['page_text'];
		$pageText = xplugin::getPluginCode($pageText);
		$texte = explode('{BOTTOM}', $pageText);

		$refPage->assign('textTop', $texte[0]);
		if (isset($texte[1]))
			$refPage->assign('textBottom', $texte[1]);
	}
	else
	{ // Normal page (category / article)
		$cols = Array('category_description', 'category_name', 'category_metadescription',
		 'category_metakey', 'category_browsepage');
		db::getInstance()->where('category_id', $title);
		$resultx = db::getInstance()->getOne("category", null, $cols);

		// Create metakeys and -description
		if (!empty($resultx['category_metakey']))
		{
			$meta = $resultx['category_metakey'] . ', ' . META_KEYWORDS;
			$metax = explode(',', $meta);
			$meta = '';

			for ($i = 0; $i < META_KEYWORD_NUMBER; $i++)
			{
				$meta .= $metax[$i];
				if ($i < META_KEYWORD_NUMBER - 1)
					$meta .=',';
			}
			$refPage->assign('Metakeywords', $meta);
		}

		if (!empty($resultx['category_metadescription']))
		{
			$meta = $resultx['category_metadescription'] . ', ' . META_DESCRIPTION;
			$meta = substr($meta, 0, META_DESCRIPTION_LENGH - 1) . '&hellip;';
			$refPage->assign('Metadescription', $meta);
		}

		$refPage->assign('header', $resultx['category_name']);
		$texte = array();
		$pageText = $resultx['category_description'];
		$pageText = xplugin::getPluginCode($pageText);
		$texte = explode('{BOTTOM}', $pageText);

		$refPage->assign('textTop', $texte[0]);

		if (count($texte) == 2)
		{
			$refPage->assign('textBottom', $texte[1]);
		}
		else
		{
			$refPage->assign('textBottom', '');
		}
	}

	if (count($result) >= 1)
	{ // List categorys
		$categoryList = array();
		foreach ($result as $row)
		{
			$categoryList[] = array('text' => $row['category_description'],
			 'link' => '/index.php?page=category&title=' . $row['category_id'] .
			 '&name=' . $row['category_name'],
			 'name' => $row['category_name'],
			 'image' => '/images/' . CATEGORY_IMAGE_PATH . '/' . $row['category_id'] . '/' .
			 CATEGORY_IMAGE_THUMB_PATH . '/' . $row['category_small_image']);
		}
		$refPage->assign('categorys', $categoryList);
		$refPage->addtpl('content', '/category.tpl.php');
	}
	else
	{ // List article from the selectet category
		$cols = Array('category_display_products');
		db::getInstance()->where('category_id', $title);
		$toShowx = db::getInstance()->getOne("category", null, $cols);
		$toShow = $toShowx['category_display_products'];

		db::getInstance()->where('product_category', $title);
		db::getInstance()->where('product_active', '1');
		db::getInstance()->orderBy('product_order', 'ASC');
		$result = db::getInstance()->get("product");

		$pagefile = 'module/function/pagecounter.php';
		if (is_file($pagefile))
			include_once($pagefile);

		$articleList = array();
		$begin = 1;
		$two = 0;
		$three = 0;
		$four = 0;

		foreach ($result as $row)
		{
			if ($displayStart <= $displayEnd and $begin == $displayStart)
			{
				$price = array();
				$tax = array();
				$price = getSingleArticlePriceWithTax($row['product_price_id']);
				$pricex = floatval(str_replace(',', '.', str_replace('.', '', $price['productPrice'])));
				$tax = getArticleTax($row['product_tax_id'], $pricex);
				$two ++;
				$three ++;
				$four ++;
				if ($pricex > 0)
				{
					$taxx = $tax['productTax'];
					$taxValuex = $tax['productTaxValue'];
				}
				else
				{
					$taxx = '0,00';
					$taxValuex = '0';
				}


				$articleList[] = array('text' => $row['product_short_description'],
				 'link' => '/index.php?page=article&title=' . $row['product_id'] .
				 '&name=' . $row['product_name'],
				 'name' => $row['product_name'],
				 'image' => '/images/' . ARTICLE_IMAGE_PATH . '/' . $title . '/' .
				 ARTICLE_IMAGE_THUMB_PATH . '/' . $row['product_small_image'],
				 'onStock' => $row['product_stocks_quantity'],
				 'sales' => $row['product_sales'],
				 'price' => $price['productPrice'],
				 'productUnit' => $price['productUnit'],
				 'tax' => $taxx,
				 'taxValue' => $taxValuex,
				 'Currency' => CURRENCY,
				 'two' => $two,
				 'three' => $three,
				 'four' => $four);

				if ($two == 2)
					$two = 0;
				if ($three == 3)
					$three = 0;
				if ($four == 4)
					$four = 0;

				$displayStart++;
				$begin++;
			}
			else
			{
				$begin++;
			}
			//TODO: Create link to buy!?
		}
		$refPage->assign('header', $resultx['category_name']);
		$refPage->assign('articleList', $articleList);
		$refPage->addtpl('content',
			'category.browse/' . $resultx['category_browsepage'] . '.tpl.php');
	}
}

?>
